package com.cn.yugan.Engine.Http;

import android.app.Activity;
import android.graphics.Movie;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.ImgUtils;
import com.cn.yugan.Widget.GifView.GifView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;

/**
 * Created by Rover on 2016/10/13 .
 * Email 357319517@qq.com
 */
public class LoadUserface {

    public static void getUserface(final Activity activity, String url, @Nullable final ImageView[] imageView, @Nullable final GifView[] gifView) {
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.get(activity, url, null, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int i, Header[] headers, final byte[] bytes) {
                        try {
                            LogUtils.d(new String(bytes, "UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        if (gifView != null) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    for (GifView gifView1 : gifView) {
                                        gifView1.setMovie(Movie.decodeByteArray(bytes, 0, bytes.length));
                                    }
                                }
                            });

                        } else if (imageView != null) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    for (ImageView imageView1 : imageView) {
                                        imageView1.setImageDrawable(ImgUtils.Bytes2Drawable(bytes));
                                    }
                                }

                            });

                        }
                    }

                    @Override
                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                        try {
                            if (bytes != null) {
                                LogUtils.d(new String(bytes, "UTF-8"));
                            } else {
                                LogUtils.d("error");
                            }
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                for (ImageView imageView1 : imageView) {
                                    imageView1.setImageResource(R.mipmap.default_img);
                                }
                            }

                        });
                    }
                }

        );
    }
}
